/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package it.univaq.iw.jweb;

import it.univaq.iw.jweb.data.DataAccess;
import it.univaq.iw.jweb.utils.HTMLHelpers;
import it.univaq.iw.jweb.utils.TemplateManager;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Map;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author Stefano
 */
public class Registrazione extends HttpServlet {

    private DataAccess da;
    private TemplateManager tpl;

    @Override
    public void init(ServletConfig config) throws ServletException {
        super.init(config);

        da = DataAccess.getInstance();

        tpl = TemplateManager.getInstance(getServletContext());
        tpl.setOutlineTemplate("outline");
        tpl.setDefaultData("logged", false);
        tpl.setDefaultData("author", "Del Vecchio - Bevini - Delfino");
        tpl.setDefaultData("homepage_url", "home");

    }

    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");

            PrintWriter out = response.getWriter();

            Map datamodel = tpl.getDefaultDataModel();

            try {

                if (!request.getParameterMap().containsKey("t")) {

                    tpl.process("registrazione",datamodel, out);

                } else {

                    if (((String)request.getParameter("t")).equalsIgnoreCase("c")) {

                        tpl.process("formRegistrazioneCandidato", datamodel, out);
                    } else {

                        tpl.process("formRegistrazioneAzienda", datamodel, out);
                    }
                }
            } catch (ServletException ex) {
                HTMLHelpers.makeErrorPage(tpl, ex.getMessage(), out);
            } finally {
                out.close();
            }
        
    } 

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    } 

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}
